package com.sy.simpleegg.platform.kkb.network.observer

import io.reactivex.Observer
import io.reactivex.disposables.Disposable

/**
 * @author sy
 * @date 2024/10/11 15:19
 * @desc
 */
abstract class BaseResultObserver<T>: Observer<T> {

    var disposable: Disposable? = null

    override fun onSubscribe(d: Disposable) {
        disposable = d
        this.onStart()
    }

    override fun onError(e: Throwable) {
        onFailure(e)
        onComplete()
    }

    override fun onComplete() {
        this.onEnd()
        this.close()
    }

    override fun onNext(t: T & Any) {
        this.onSuccess(t)
    }

    private fun close() {
        if (disposable != null && !disposable!!.isDisposed) {
            disposable!!.dispose()
        }
    }

    fun onStart() {}

    fun onEnd() {}

    abstract fun onSuccess(var1: T)

    abstract fun onFailure(var1: Throwable?)
}